/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoProgramaDialog.java
 *
 * Created on 18/06/2013, 06:08:44 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import java.awt.Component;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import org.jdesktop.observablecollections.ObservableCollections;
import pe.edu.unmsm.cinfo.siga.datos.Modulo;
import pe.edu.unmsm.cinfo.siga.datos.Programa;
import pe.edu.unmsm.cinfo.siga.datos.ProgramaModulo;
import pe.edu.unmsm.cinfo.siga.datos.ProgramaModuloPK;
import pe.edu.unmsm.cinfo.siga.ui.utils.EstadoRenderer;
import pe.edu.unmsm.cinfo.siga.ui.utils.ShortToBooleanConverter;
import pe.edu.unmsm.cinfo.siga.ui.utils.ValidadorPrograma;

/**
 *
 * @author alumno
 */
public class NuevoProgramaDialog extends javax.swing.JDialog {
    
    private boolean entityConfirmed=false;
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public class EstadoEditor extends DefaultCellEditor{
        private JComboBox comboBox;

        public EstadoEditor(){
            this(new JComboBox());
        }
        public EstadoEditor(JComboBox comboBox) {
            super(comboBox);
            this.comboBox = comboBox;
            comboBox.setModel(new DefaultComboBoxModel(new Short[]{0, 1}));
            this.comboBox.setRenderer(new DefaultListCellRenderer(){
                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    if (value != null && value.equals((short) 1)) {
                        setText("Activo");
                    } else {
                        setText("Inactivo");
                    }
                    return this;
                }
            });
        }
    }
    
    /** Creates new form NuevoProgramaDialog */
    public NuevoProgramaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        establecerOyentes();
    }
    
    private void establecerOyentes(){
        modulosDisponiblesList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("moduloDisponibleSeleccionado", 
                        !isModuloDisponibleSeleccionado(),
                        isModuloDisponibleSeleccionado());
            }
        });
        cursosAsignadosTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                changeSupport.firePropertyChange("moduloAsignadoSeleccionado",
                        !isModuloAsignadoSeleccionado(),
                        isModuloAsignadoSeleccionado());
            }
        });
    }

    public boolean isEntityConfirmed() {
        return entityConfirmed;
    }

    public void setEntityConfirmed(boolean entityConfirmed) {
        this.entityConfirmed = entityConfirmed;
    }
    
    public Programa getPrograma() {
        return programa;
    }

    public void setPrograma(Programa programa) {
        assert programa != null;
        if(programa.getProgramaModuloList() == null){
            programa.setProgramaModuloList(new LinkedList<ProgramaModulo>());
        }
        Programa old = this.programa;
        this.programa = programa;
        changeSupport.firePropertyChange("programa", old, programa);
        setModulosPrograma(programa.getProgramaModuloList());
        filtrarModulosDisponibles();
    }

    public List<ProgramaModulo> getModulosPrograma() {
        return modulosPrograma;
    }

    private void setModulosPrograma(List<ProgramaModulo> modulosPrograma) {
        List<ProgramaModulo> old = getModulosPrograma();
        this.modulosPrograma = ObservableCollections.observableList(modulosPrograma);
        changeSupport.firePropertyChange("modulosPrograma", old, getModulosPrograma());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacionSIGAPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(NuevoProgramaDialog.class);
        modulosQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("modulosQuery.query")); // NOI18N
        modulosList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(modulosQuery.getResultList());
        programa = null;
        modulosPrograma = null;
        nombreTextField = new javax.swing.JTextField();
        estaActivoCheckbox = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        contadoTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        numPagosSpinner = new javax.swing.JSpinner();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        precioPagoTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        modulosDisponiblesList = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        cursosAsignadosTable = new javax.swing.JTable();
        asignarButton = new javax.swing.JButton();
        desasignarButton = new javax.swing.JButton();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        codigoTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setModal(true);
        setName("Form"); // NOI18N

        nombreTextField.setName("nombreTextField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proNom}"), nombreTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        estaActivoCheckbox.setText("Activo"); // NOI18N
        estaActivoCheckbox.setName("estaActivoCheckbox"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proEst}"), estaActivoCheckbox, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        binding.setSourceNullValue(false);
        binding.setSourceUnreadableValue(false);
        binding.setConverter(ShortToBooleanConverter.get());
        bindingGroup.addBinding(binding);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Precio:"));
        jPanel3.setName("jPanel3"); // NOI18N

        contadoTextField.setName("contadoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proPre}"), contadoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Numero de pagos:"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        numPagosSpinner.setModel(new javax.swing.SpinnerNumberModel(Short.valueOf((short)0), Short.valueOf((short)0), null, Short.valueOf((short)1)));
        numPagosSpinner.setName("numPagosSpinner"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proNpar}"), numPagosSpinner, org.jdesktop.beansbinding.BeanProperty.create("value"));
        binding.setSourceNullValue((short)0);
        binding.setSourceUnreadableValue((short)0);
        bindingGroup.addBinding(binding);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Precio por pago:"); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        precioPagoTextField.setName("precioPagoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proPar}"), precioPagoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(contadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(numPagosSpinner)
                            .addComponent(precioPagoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel7))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(contadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(numPagosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(precioPagoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(6, Short.MAX_VALUE))
        );

        jLabel5.setText("Nombre:"); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        modulosDisponiblesList.setName("modulosDisponiblesList"); // NOI18N

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, modulosList, modulosDisponiblesList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${modDesc}"));
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(modulosDisponiblesList);

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel6.setText("Modulos disponibles:"); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        cursosAsignadosTable.setName("cursosAsignadosTable"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${modulosPrograma}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, cursosAsignadosTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${promOrd}"));
        columnBinding.setColumnName("Prom Ord");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modulo.modDesc}"));
        columnBinding.setColumnName("Modulo.mod Desc");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${promEst}"));
        columnBinding.setColumnName("Prom Est");
        columnBinding.setColumnClass(Short.class);
        jTableBinding.setSourceNullValue(java.util.Collections.EMPTY_LIST);
        jTableBinding.setSourceUnreadableValue(java.util.Collections.EMPTY_LIST);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(cursosAsignadosTable);
        cursosAsignadosTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title2")); // NOI18N
        cursosAsignadosTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title1")); // NOI18N
        cursosAsignadosTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("cursosAsignadosTable.columnModel.title0")); // NOI18N
        cursosAsignadosTable.getColumnModel().getColumn(2).setCellEditor(new EstadoEditor());
        cursosAsignadosTable.getColumnModel().getColumn(2).setCellRenderer(new EstadoRenderer());

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(NuevoProgramaDialog.class, this);
        asignarButton.setAction(actionMap.get("addModulo")); // NOI18N
        asignarButton.setName("asignarButton"); // NOI18N

        desasignarButton.setAction(actionMap.get("removeModulo")); // NOI18N
        desasignarButton.setIcon(resourceMap.getIcon("desasignarButton.icon")); // NOI18N
        desasignarButton.setName("desasignarButton"); // NOI18N

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Codigo:"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        codigoTextField.setEditable(false);
        codigoTextField.setName("codigoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programa.proCod}"), codigoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jButton1.setAction(actionMap.get("save")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("cancel")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(estaActivoCheckbox))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                            .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(asignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(desasignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(nombreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(estaActivoCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(asignarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(desasignarButton))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    @Action
    public void save() {
        ValidadorPrograma validador = new ValidadorPrograma(programa);
        if(validador.esValido()){
            this.setEntityConfirmed(true);
            this.setVisible(false);
        }else{
            JOptionPane.showMessageDialog(this, validador.getMensaje(), "SIGA-CINFO", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @Action
    public void cancel() {
        this.setEntityConfirmed(false);
        this.setVisible(false);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener l){
        changeSupport.addPropertyChangeListener(l);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener l){
        changeSupport.removePropertyChangeListener(l);
    }

    @Action(enabledProperty = "moduloDisponibleSeleccionado")
    public void addModulo() {
        Modulo m = obtenerModuloDisponibleSeleccionado();
        getModulosPrograma().add(crearNuevoModuloPrograma(m));
        modulosList.remove(m);
    }
    
    private Modulo obtenerModuloDisponibleSeleccionado(){
        int indice=modulosDisponiblesList.getSelectedIndex();
        return modulosList.get(indice);
    }
    
    private ProgramaModulo crearNuevoModuloPrograma(Modulo m){
        ProgramaModulo pm=new ProgramaModulo();
        pm.setProgramaModuloPK(new ProgramaModuloPK());
        pm.getProgramaModuloPK().setModCod(m.getModCod());
        pm.setModulo(m);
        pm.setPrograma(programa);
        pm.setPromOrd(getModulosPrograma().size()+1);
        pm.setPromEst((short)1);
        return pm;
    }

    @Action(enabledProperty = "moduloAsignadoSeleccionado")
    public void removeModulo() {
        ProgramaModulo pm = obtenerModuloProgramaSeleccionado();
        getModulosPrograma().remove(pm);
        modulosList.add(pm.getModulo());
    }

    private ProgramaModulo obtenerModuloProgramaSeleccionado(){
        int indice = cursosAsignadosTable.convertRowIndexToModel(cursosAsignadosTable.getSelectedRow());
        return programa.getProgramaModuloList().get(indice);
    }
    
    private void filtrarModulosDisponibles(){
        if(programa == null) return;
        for(ProgramaModulo elemento : getModulosPrograma()){
            modulosList.remove(elemento.getModulo());
        }
    }

    public boolean isModuloDisponibleSeleccionado() {
        return modulosDisponiblesList.getSelectedIndex() != -1;
    }

    public boolean isModuloAsignadoSeleccionado() {
        return cursosAsignadosTable.getSelectedRow() != -1;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton asignarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.swing.JTextField contadoTextField;
    private javax.swing.JTable cursosAsignadosTable;
    private javax.swing.JButton desasignarButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JCheckBox estaActivoCheckbox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList modulosDisponiblesList;
    private java.util.List<Modulo> modulosList;
    private java.util.List<ProgramaModulo> modulosPrograma;
    private javax.persistence.Query modulosQuery;
    private javax.swing.JTextField nombreTextField;
    private javax.swing.JSpinner numPagosSpinner;
    private javax.swing.JTextField precioPagoTextField;
    private pe.edu.unmsm.cinfo.siga.datos.Programa programa;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
